/* 
 * @author : Amissah Gideon<kuulmek@yahoo.com>
 * @version 1.0
 */


var BillPayment = {
    
    init:function(){ 
       $("#paybill-form").validationEngine();
       $('#paybill-save-btn').click(function(){
           BillPayment.save();
       });
       $('#paybill-cancel-btn').click(function(){
           document.getElementById('paybill-form').reset();
           $.validationEngine.closePrompt('.formError',true);
       });
       
       $('.bill_info').live('click',function(){
            var id = $(this).parent().parent().attr('data-id');
            var property_id = $(this).parent().parent().attr('property-id');
            BillPayment.veiwBillInfo(id,property_id);
       });
	},
    
    save:function(){
         /* Validate the form*/
          var validationStatus =  $('#paybill-form').validationEngine({returnIsValid:true});
           /* When the Validation Status is true meaning that the data input are correct then the data can be processed. */
          if(validationStatus){
                 var query = 'model=BillPayment&'+$('#paybill-form').serialize();
                 /* Send the data to the server and handle the server response */                     
                 $.post( $('#pay_bill-url').val(), query, function(response){
					var txt = '';
                    if(typeof response.mesg  == 'object'){
    					for(megTxt in response.mesg){
    						txt += response.mesg[megTxt]+'<br />';
    					}
    				}
    				else{
    					txt = response.mesg
    				}
                    
					if(response.code === 0){					   
					   jLib.setFeedback(txt, 'success_message', false); 
                       document.getElementById('paybill-form').reset();
                       $.validationEngine.closePrompt('.formError',true);
                       
                       if($('#print_receipt_chk').is(":checked")){
                           BillPayment.printReceipt(response.data_id);
                       }
					}
					else if(response.code === 1){
					   jLib.builPopUpPrompt('.bill-prompt',txt,'error',false);
					   jLib.setFeedback(txt, 'error_message', false);
					}
                },'json'); 
          } 
   },
   
   printReceipt:function(id){    
        var url = $('#print-receipt-url').val()+'/'+id;
        window.open(url,"PrintWindow","menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
	},
    
    veiwBillInfo:function(id, property_id){
         var url = $('#bill-payment-info-url').val();
         var query = 'id=' + id + '&property_id='+property_id;
         $.post(url, query, function(response){  
    		   $("#billPaymentInfo_colorbox").html(response);
               
               $('table.list-table-data tbody tr:even').addClass('table_zebra');  
               
               $.colorbox({
                 inline:true,
                 scrolling:false,
                 overlayClose:false,
                 escKey:false,
                 title:'Bill Payment Info',
                 href:"#billPaymentInfo_colorbox",
                 width:"55%", 
                 height:"50%"
              });
              
              $.colorbox.resize();
         },'html');
    }
   
   
};

/* when the page is loaded */
$(document).ready(function() {
	BillPayment.init();
});